/***************************************************************************
 *    
 *      Copyright (c) 2009,2010 KaiTrade LLC (registered in Delaware)
 *                     All Rights Reserved Worldwide
 *
 * STRICTLY PROPRIETARY and CONFIDENTIAL
 *
 * WARNING:  This file is the confidential property of KaiTrade LLC For
 * use only by those with the express written permission and license from
 * KaiTrade LLC.  Unauthorized reproduction, distribution, use or disclosure 
 * of this file or any program (or document) is prohibited. 
 * 
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace K2AlgoSet
{
    /// <summary>
    /// this class provides and ATR calculation
    /// </summary>
    class ATR : ExpressionBase
    {
        private int m_SampleSize = 0;
        private K2AlgoSet.IMovingAverage m_MATrueRange;
        private string m_CurveName = "";

        public ATR()
        {
            m_Name = "KATR";
            m_Log = log4net.LogManager.GetLogger("PlugIn");
        }

        /// <summary>
        /// THis method is used to parse the parameters used in this calculation
        /// </summary>
        /// <param name="myExpression"></param>
        protected override void parseExpression(string myExpression)
        {
            try
            {
                string[] myParms = myExpression.Split(',');
                string m_CurveName = myParms[0];
                string mySize = myParms[1];
                m_SampleSize = int.Parse(mySize);
                m_MATrueRange = new K2AlgoSet.SimpleMovingAverage(m_SampleSize);
                 

            }
            catch (Exception myE)
            {
                m_Log.Error("parseExpression", myE);
            }
        }

        /// <summary>
        /// Returns the current value - this assumes that an expression returns a single value
        /// </summary>
        public override double Value
        {
            get { return (double)m_MATrueRange.Average; }
        }

        /// <summary>
        /// Evaluate the expression on the current data, normally should use
        /// the last complete item/bar - setting bUseCurrent bar will cause
        /// the evaluation to include the bar being added to, that has yet to
        /// end.
        /// </summary>
        public override void EvaluateAdded(bool bUseCurrentBar)
        {
            try
            {

                if (bUseCurrentBar)
                {
                    // since we are using the current bar then just replace the
                    // current value of the moving average - note this can change
                    // manay times per period
                    KaiTrade.Interfaces.TSItem myBar = m_TSSet.CurrentItem;
                    if (myBar != null)
                    {
                        m_MATrueRange.AddSample((float)myBar.TrueRange);
                        
                    }
                }
                else
                {
                    // Since we are using the last complete just roll that into the
                    // average
                    KaiTrade.Interfaces.TSItem myBar = m_TSSet.LastCompleteItem;
                    if (myBar != null)
                    {
                        m_MATrueRange.AddSample((float)myBar.TrueRange);
                    }
                }

            }
            catch (Exception myE)
            {
                m_Log.Error("Evaluate", myE);
            }
        }

        /// <summary>
        /// Evaluate the expression after some intrabar update has occured.
        /// </summary>
        /// <param name="bUseCurrentBar"></param>
        public override void EvaluateUpdate(bool bUseCurrentBar)
        {
            try
            {

                if (bUseCurrentBar)
                {
                    // since we are using the current bar then just replace the
                    // current value of the moving average - note this can change
                    // manay times per period
                    KaiTrade.Interfaces.TSItem myBar = m_TSSet.CurrentItem;
                    if (myBar != null)
                    {
                        m_MATrueRange.SetCurrentValue((float)myBar.TrueRange);
                        
                    }
                }
                else
                {
                    /*
                    KaiTrade.Interfaces.TSItem myBar = m_TSSet.LastCompleteItem;
                    if (myBar != null)
                    {
                         m_MATrueRange.SetCurrentValue((float)myBar.TrueRange);
                    }
                     */
                }

            }
            catch (Exception myE)
            {
                m_Log.Error("Evaluate", myE);
            }
        }



    }
}
